<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名称" prop="gsmc">
        <el-input
          v-model="queryParams.gsmc"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总部编号" prop="zbbh">
        <el-input
          v-model="queryParams.zbbh"
          placeholder="请输入总部编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属公司" prop="gsmc">
        <el-select v-model="queryParams.createDeptName" style="width: 100%;" placeholder="请选择所属公司">
          <el-option
            v-for="item in ssgsList"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="月份" prop="yf">
        <el-select v-model="queryParams.yfStr" multiple style="width: 100%;" placeholder="请选择">
          <el-option
            v-for="item in yfList"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gd:ldjl:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gd:ldjl:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleAllDelete"
          v-hasPermi="['gd:ldjl:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gd:ldjl:export']"
        >全部导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleSelectExport"
          v-hasPermi="['gd:ldjl:export']"
        >导出选中数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ldjlList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="总部编号" align="left" prop="zbbh">
        <template slot-scope="scope">
          <div style="color: #00afff;cursor: pointer;" @click="showInfo(scope.row)">{{scope.row.zbbh}}</div>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="left" prop="gsmc" />
      <el-table-column label="原始记录" align="center" prop="ysjl" />
      <el-table-column label="实际录入" align="center" prop="sjlr" />
      <el-table-column label="日期" align="center" prop="rq" />
      <el-table-column label="所属公司" align="center" prop="createDeptName">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.createDeptName === 'szzb'" type="success">深圳总部</el-tag>
          <el-tag v-else type="warning">天津分公司</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="录入时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gd:ldjl:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gd:ldjl:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <formInfo v-if="open" :open="open" :ShowBtn="ShowBtn" :title="title" :formInfo="form" @close="cancel"></formInfo>

  </div>
</template>

<script>
import { listLdjl, getLdjl, delLdjl, addLdjl, updateLdjl } from "@/api/gd/ldjl";
import { parseTime } from '@/utils/ruoyi'
import { delFj, allListFj, getFj } from '@/api/gd/fj'
import { getToken } from '@/utils/auth'
import formInfo from '@/views/gd/ldjl/formInfo.vue'

export default {
  name: "Ldjl",
  components: { formInfo },
  data() {
    return {
      showDialog: false,
      fjList: [],
      src: this.uploadSrc + '/gd/fj/file/upload',
      uploadHeaders: { 'Authorization': 'Bearer ' + getToken() }, // 如果需要CSRF保护的话
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 录单记录表格数据
      ldjlList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      ShowBtn: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        zbbh: undefined,
        gsmc: undefined,
        ysjl: undefined,
        sjlr: undefined,
        yfStr: undefined,
        createId: undefined,
        createDept: undefined,
        createDeptName: undefined
      },
      // 表单参数
      form: {},
      yfList: [],
      ssgsList: []
    };
  },
  created() {
    this.getDict()
    this.getList();
  },
  methods: {
    getDict() {
      // 月份
      this.getDicts("gd_yf").then(res => {
        this.yfList = res.data
      })
      // 所属公司
      this.getDicts("gd_ssgs").then(res => {
        this.ssgsList = res.data
      })
    },
    /** 查询录单记录列表 */
    getList() {
      this.loading = true;
      listLdjl(this.queryParams).then(response => {
        this.ldjlList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.getList()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        zbbh: undefined,
        gsmc: undefined,
        ysjl: undefined,
        sjlr: undefined,
        yf: undefined,
        createBy: this.$store.getters.name,
        createTime: parseTime(new Date()),
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        createId: undefined,
        createDept: undefined,
        createDeptName: undefined
      };
      this.resetForm("form");
      this.fjList = []
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          zbbh: undefined,
          gsmc: undefined,
          ysjl: undefined,
          sjlr: undefined,
          yf: undefined,
          createId: undefined,
          createDept: undefined,
          createDeptName: undefined
      }
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.ShowBtn = true
      this.title = "添加录单记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      let than = this
      getLdjl(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.ShowBtn = true
        this.title = "修改录单记录";
      });
    },
    showInfo(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getLdjl(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.ShowBtn = false
        this.title = "修改录单记录";
      });
    },
    handleAllDelete() {
      const ids = this.ids;
      const data = this.ldjlList.filter(item => ids.some(id => id === item.id))
      let obj = ""
      data.forEach((item, index) => {
        if (index === data.length-1) {
          obj += "编号为：" +item.zbbh + " 公司名称为： " + item.gsmc
        }else {
          obj += "编号为：" +item.zbbh + " 公司名称为： " + item.gsmc + ", "
        }
      })
      this.$modal.confirm('请确认是否删除 ' + obj + ' 的数据?').then(() => {
        this.loading = true;
        return delLdjl(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除录单记录编号为"' + row.zbbh + " 公司名称为： " + row.gsmc +  '"的数据项？').then(() => {
        this.loading = true;
        return delLdjl(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gd/ldjl/export', {
        ...this.queryParams
      }, `录单记录_${new Date().getTime()}.xlsx`)
    },
    handleSelectExport() {
      const ids = this.ids;
      this.download('gd/ldjl/exportSelectDat/' + ids, {
        ...this.queryParams
      }, `完工单_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
